<script setup>
import {
  getUserInfoAPI,
  getAllUserInfoAPI,
  disableUserAPI,
  enableUserAPI
} from '@/api/user'
import { onMounted } from 'vue'
import { ref } from 'vue'
const name = ref('')

const selectUserInfo = ref([])
const showSelectUser = ref(false)
const getUserInfo = async () => {
  const res = await getUserInfoAPI(name.value)
  console.log(res)
  if (res.data == '') {
    ElMessage.error('未查询到该用户')
  } else {
    selectUserInfo.value = []
    selectUserInfo.value.push(res.data)
    showSelectUser.value = true
  }
}
const allUserInfo = ref([])
const getAllUserInfo = async () => {
  const res = await getAllUserInfoAPI()
  allUserInfo.value = res.data
  if (allUserInfo.value.length > pageSize.value) {
    nowPageInfo.value = allUserInfo.value.slice(index.value, end.value)
  } else {
    nowPageInfo.value = allUserInfo.value
  }
}

const disableUser = async (UserName) => {
  const res = await disableUserAPI(UserName)
  console.log(res)
  if (res.data === 1) {
    getAllUserInfo()
    showSelectUser.value = false
    ElMessage.success('禁用成功')
  } else {
    ElMessage.error('禁用失败')
  }
}
const enableUser = async (UserName) => {
  const res = await enableUserAPI(UserName)
  console.log(res)
  if (res.data === 1) {
    getAllUserInfo()
    showSelectUser.value = false
    ElMessage.success('启用成功')
  } else {
    ElMessage.error('启用失败')
  }
}

onMounted(() => {
  getAllUserInfo()
})
const currentPage = ref(1)
const pageSize = ref(10)
const index = ref((currentPage.value - 1) * pageSize.value)
const end = ref(index.value + pageSize.value)
const nowPageInfo = ref([])
const handleSizeChange = (val) => {
  pageSize.value = val
  console.log(`${val} items per page`)
}
const handleCurrentChange = (val) => {
  console.log(`current page: ${val}`)
  currentPage.value = val
  index.value = (currentPage.value - 1) * pageSize.value
  end.value = index.value + pageSize.value
  console.log(index.value, end.value)
  nowPageInfo.value = allUserInfo.value.slice(index.value, end.value)
}
</script>

<template>
  <el-input
    v-model="name"
    style="width: 240px; margin: 10px"
    placeholder="输入用户名"
  />
  <el-button type="primary" @click="getUserInfo()">搜索</el-button>
  <el-table :data="nowPageInfo" border="teue" style="width: 100%">
    <el-table-column prop="UserAccount" label="账号" width="180" />
    <el-table-column prop="UserName" label="用户名" width="180" />
    <el-table-column prop="UserPhoneNumber" label="手机号" />
    <el-table-column prop="UserSex" label="性别" />
    <el-table-column label="账号状态">
      <template #default="scope">
        <!-- 根据 Status 的值显示不同文本 -->
        {{ scope.row.Status === 1 ? '正常' : '封禁' }}
      </template>
    </el-table-column>
    <el-table-column label="操作" width="200">
      <template #default="scope">
        <!-- 根据 Status 的值显示不同的按钮 -->
        <el-button
          type="danger"
          v-if="scope.row.Status === 1"
          @click="disableUser(scope.row.UserName)"
          >封禁</el-button
        >
        <el-button type="success" v-else @click="enableUser(scope.row.UserName)"
          >解封</el-button
        >
      </template>
    </el-table-column>
  </el-table>
  <el-pagination
    v-model:current-page="currentPage"
    v-model:page-size="pageSize"
    :page-sizes="[100, 200, 300, 400]"
    layout="sizes, prev, pager, next"
    :total="allUserInfo.length"
    @size-change="handleSizeChange"
    @current-change="handleCurrentChange"
    background
    style="margin: 10px auto"
  />
  <el-dialog v-model="showSelectUser" title="用户信息" width="1000">
    <el-table :data="selectUserInfo" border="teue" style="width: 100%">
      <el-table-column prop="UserAccount" label="账号" width="180" />
      <el-table-column prop="UserName" label="用户名" width="180" />
      <el-table-column prop="UserPhoneNumber" label="手机号" />
      <el-table-column prop="UserSex" label="性别" />
      <el-table-column label="账号状态">
        <template #default="scope">
          <!-- 根据 Status 的值显示不同文本 -->
          {{ scope.row.Status === 1 ? '正常' : '封禁' }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template #default="scope">
          <!-- 根据 Status 的值显示不同的按钮 -->
          <el-button
            type="danger"
            v-if="scope.row.Status === 1"
            @click="disableUser(scope.row.UserName)"
            >封禁</el-button
          >
          <el-button
            type="success"
            v-else
            @click="enableUser(scope.row.UserName)"
            >解封</el-button
          >
        </template>
      </el-table-column>
    </el-table>
  </el-dialog>
</template>

<style></style>
